#include <string>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "Log.hpp"
#include <cstring>
int main(int argc, char* argv[])
{
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        exit(1);
    }
    lg.LogMessage(Debug, "socket create success\n");

    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);

    struct sockaddr_in server; //#include <netinet/in.h>
    memset(&server, 0, sizeof(server)); //
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ip.c_str());
    server.sin_port = htons(port);

    while(true)
    {
        std::string inbuffer;
        std::cout << "client enter: ";
        getline(std::cin, inbuffer);

        int n = sendto(sockfd, inbuffer.c_str(), inbuffer.size(), 0, (struct sockaddr*)&server, sizeof server);
        if(n > 0) 
        {
            lg.LogMessage(Debug, "sendto success\n");

            char getbuffer[1024];
            struct sockaddr_in temp;
            socklen_t len = sizeof temp;
            int m = recvfrom(sockfd, getbuffer, sizeof getbuffer - 1, 0, (struct sockaddr*)&temp, &len);
            if(m > 0)
            {
                lg.LogMessage(Debug, "recvfrom success\n");

                getbuffer[m] = 0;
                std::cout << "server: " << getbuffer << std::endl;
            }
        }
    }
    close(sockfd);
}